package cineplanet.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import javax.sql.DataSource;

import cineplanet.modelo.Pelicula;
import cineplanet.excepcion.DAOExcepcion;

public class PeliculaDAOImpl extends BaseDAO implements PeliculaDAO {

	private DataSource dataSource;

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public Collection<Pelicula> buscarPorNombre(String titulo)
			throws DAOExcepcion {
		String query = "select idpelicula,titulo,genero,duracion,pais,director,actores,censura,web,sinopsis,tresd,estreno,estado,portada,tipoProyeccion from pelicula where titulo like ?";
		Collection<Pelicula> lista = new ArrayList<Pelicula>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			stmt = con.prepareStatement(query);
			stmt.setString(1, "%" + titulo + "%");
			rs = stmt.executeQuery();
			while (rs.next()) {
				Pelicula vo = new Pelicula();
				vo.setIdPelicula(rs.getInt("idPelicula"));
				vo.setTitulo(rs.getString("titulo"));
				vo.setGenero(rs.getString("genero"));
				vo.setDuracion(rs.getString("duracion"));
				vo.setPais(rs.getString("pais"));
				vo.setDirector(rs.getString("director"));
				vo.setActores(rs.getString("actores"));
				vo.setCensura(rs.getString("censura"));
				vo.setWeb(rs.getString("web"));
				vo.setSinopsis(rs.getString("sinopsis"));
				vo.setTresd(rs.getByte("tresd"));
				vo.setEstreno(rs.getByte("estreno"));
				vo.setEstado(rs.getByte("estado"));
				vo.setPortada(rs.getByte("portada"));
				vo.setTipoProyeccion(rs.getString("tipoProyeccion"));
				lista.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		System.out.println(lista.size());
		return lista;
	}

	public Pelicula insertar(Pelicula vo) throws DAOExcepcion {
		String query = "INSERT INTO pelicula(titulo,genero,duracion,pais,director,actores,censura,web,sinopsis,tresd,estreno,estado,portada,tipoProyeccion) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getTitulo());
			stmt.setString(2, vo.getGenero());
			stmt.setString(3, vo.getDuracion());
			stmt.setString(4, vo.getPais());
			stmt.setString(5, vo.getDirector());
			stmt.setString(6, vo.getActores());
			stmt.setString(7, vo.getCensura());
			stmt.setString(8, vo.getWeb());
			stmt.setString(9, vo.getSinopsis());
			stmt.setByte(10, vo.getTresd());
			stmt.setByte(11, vo.getEstreno());
			stmt.setByte(12, vo.getEstado());
			stmt.setByte(13, vo.getPortada());
			stmt.setString(14, vo.getTipoProyeccion());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			} // Obtener el ultimo id
			query = "SELECT LAST_INSERT_ID()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				vo.setIdPelicula(rs.getInt(1));
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public Pelicula obtener(int idPelicula) throws DAOExcepcion {
		Pelicula vo = new Pelicula();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select idPelicula,titulo,genero,duracion,pais,director,actores,censura,web,sinopsis,tresd,estreno,estado,portada,tipoProyeccion from pelicula where idPelicula=?";
			con = dataSource.getConnection();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idPelicula);
			rs = stmt.executeQuery();
			if (rs.next()) {
				vo.setIdPelicula(rs.getInt("idPelicula"));
				vo.setTitulo(rs.getString("titulo"));
				vo.setGenero(rs.getString("genero"));
				vo.setDuracion(rs.getString("duracion"));
				vo.setPais(rs.getString("pais"));
				vo.setDirector(rs.getString("director"));
				vo.setActores(rs.getString("actores"));
				vo.setCensura(rs.getString("censura"));
				vo.setWeb(rs.getString("web"));
				vo.setSinopsis(rs.getString("sinopsis"));
				vo.setTresd(rs.getByte("tresd"));
				vo.setEstreno(rs.getByte("estreno"));
				vo.setEstado(rs.getByte("estado"));
				vo.setPortada(rs.getByte("portada"));
				vo.setTipoProyeccion(rs.getString("tipoProyeccion"));

			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public void eliminar(int idPelicula) throws DAOExcepcion {
		String query = "DELETE FROM pelicula WHERE idPelicula=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = dataSource.getConnection();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idPelicula);
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public Pelicula actualizar(Pelicula vo) throws DAOExcepcion {
		// Actualizar los campos para la actualizacion
		String query = "update pelicula set titulo=?,genero=?,duracion=?,pais=?,director=?,actores=?,censura=?,web=?,sinopsis=?,tresd=?,estreno=?,estado=?,portada=?,tipoProyeccion=? where idPelicula=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = dataSource.getConnection();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getTitulo());
			stmt.setString(2, vo.getGenero());
			stmt.setString(3, vo.getDuracion());
			stmt.setString(4, vo.getPais());
			stmt.setString(5, vo.getDirector());
			stmt.setString(6, vo.getActores());
			stmt.setString(7, vo.getCensura());
			stmt.setString(8, vo.getWeb());
			stmt.setString(9, vo.getSinopsis());
			stmt.setByte(10, vo.getTresd());
			stmt.setByte(11, vo.getEstreno());
			stmt.setByte(12, vo.getEstado());
			stmt.setByte(13, vo.getPortada());
			stmt.setString(14, vo.getTipoProyeccion());
			stmt.setInt(15, vo.getIdPelicula());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	@Override
	public Collection<Pelicula> listar() throws DAOExcepcion {
		Collection<Pelicula> c = new ArrayList<Pelicula>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			// Actualizar el query para la DB
			String query = "SELECT idpelicula,titulo,genero,duracion,pais,director,actores,censura,web,sinopsis,tresd,estreno,estado,portada,tipoProyeccion from pelicula";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Pelicula vo = new Pelicula();
				vo.setIdPelicula(rs.getInt("idPelicula"));
				vo.setTitulo(rs.getString("titulo"));
				vo.setGenero(rs.getString("genero"));
				vo.setDuracion(rs.getString("duracion"));
				vo.setPais(rs.getString("pais"));
				vo.setDirector(rs.getString("director"));
				vo.setActores(rs.getString("actores"));
				vo.setCensura(rs.getString("censura"));
				vo.setWeb(rs.getString("web"));
				vo.setSinopsis(rs.getString("sinopsis"));
				vo.setTresd(rs.getByte("tresd"));
				vo.setEstreno(rs.getByte("estreno"));
				vo.setEstado(rs.getByte("estado"));
				vo.setPortada(rs.getByte("portada"));
				vo.setTipoProyeccion(rs.getString("tipoProyeccion"));

				c.add(vo);
				System.err.println(c);

			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}

}